package com.bipbip.repositorio;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.bipbip.modelo.Lugar;

@Repository(value = "lugarDao")
@Transactional(readOnly = true)
public class JPALugarDAOImpl implements LugarDAO {
	
	private EntityManager em = null;

	/*
	 * Sets the entity manager.
	 */
	@PersistenceContext
	public void setEntityManager(EntityManager em) {
		this.em = em;
	}

	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public Lugar insert(Lugar lugar) {
		em.persist(lugar);
		return lugar;
	}

	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public void delete(Lugar l) {
		em.remove(em.merge(l));
	}

	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public Lugar update(Lugar l) {
		return em.merge(l);
	}

	public Lugar select(int id) {
		return em.find(Lugar.class, id);
	}

	public List<Lugar> selectAll() {
		return em.createQuery("select l from Lugar l").getResultList();
	}

}
